package com.yinhe.music.yhmusic.login.phonelogin;

import android.text.TextUtils;

import com.hwangjr.rxbus.RxBus;
import com.umeng.analytics.MobclickAgent;
import com.yinhe.music.event.RxBusEventType;
import com.yinhe.music.yhmusic.application.MusicApplication;
import com.yinhe.music.yhmusic.base.BaseModel;
import com.yinhe.music.yhmusic.base.BasePresenter;
import com.yinhe.music.yhmusic.cache.CacheManager;
import com.yinhe.music.yhmusic.db.DataBaseAccessor;
import com.yinhe.music.yhmusic.model.LoginInfo;
import com.yinhe.music.yhmusic.network.NetworkManager;
import com.yinhe.music.yhmusic.network.response.ResponseTransformer;
import com.yinhe.music.yhmusic.utils.Preferences;
import com.yinhe.music.yhmusic.utils.SongUtil;
import com.yinhe.music.yhmusic.utils.UmengEventUtils;

/**
 * @author 何晓华
 */
public class LoginPresenter extends BasePresenter<BaseModel, LoginContract.ILoginView> implements LoginContract.ILoginPresenter {

    @Override
    public void requestLogin(String phone, String password) {

        if (getView() == null) {
            return;
        }

        if (!TextUtils.isEmpty(phone) && !TextUtils.isEmpty(password)) {
            getView().showLoading();

            addSubscription(mModel.requestLogin(phone, password)
                    .compose(ResponseTransformer.handleResult())
                    .compose(schedulerProvider.applySchedulers())
                    .subscribe(loginInfo -> {
                        UmengEventUtils.login(getView().getContext(), UmengEventUtils.PHONE);
                        getView().setLogin(loginInfo);
                        getView().hideLoading();
                    }, throwable -> {
                        getView().showMessage(throwable);
                        getView().hideLoading();
                    }));
        } else {
            getView().showToast("请输入正确账号或密码");
        }


    }

    @Override
    public void thirdPartLogin(String unionid, String platformId, String name, String gender, String image, String platform) {
        if (getView() == null) {
            return;
        }

        addSubscription(mModel.thirdPartLogin(unionid, platformId, platform, name, gender, image)
                .compose(ResponseTransformer.handleResult())
                .compose(schedulerProvider.applySchedulers())
                .subscribe(loginInfo -> {
                    getView().setLogin(loginInfo);
                    getView().hideLoading();
                }, throwable -> {
                    getView().showMessage(throwable);
                    getView().hideLoading();
                }));
    }

    @Override
    public void saveLoginInfo(LoginInfo loginInfo) {
        MobclickAgent.onProfileSignIn(String.valueOf(loginInfo.getUid()));

        DataBaseAccessor.getInstance().uninit();
        Preferences.setLoginSN(loginInfo.getSn());
        Preferences.setUid(loginInfo.getUid());

        DataBaseAccessor.getInstance().initDBHelper(MusicApplication.getInstance(), loginInfo.getUid() + ".db");

        SongUtil.saveSong(CacheManager.getInstance().getScanMusic());

        RxBus.get().post(RxBusEventType.User.LOGIN_SUCCESS, loginInfo);

        NetworkManager.getInstance().init();
    }
}
